package com.mimosa.common.system.log;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.mimosa.common.system.role.TRole;
import com.mimosa.common.system.user.TUser;
import com.mimosa.common.system.user.UserContext;

/**
 * 日志切面
 * 
 * @author yunjum
 * @2012-11-11 20:51
 */
@Aspect
public class LogAspect {
	final Logger logger = LoggerFactory.getLogger(LogAspect.class);

	@Autowired
	private LogService logService;

	@Autowired
	private UserContext userContext;

	/***
	 * 拦截用户模块
	 */

	/** 删除用户 ***/
	@AfterReturning(value = "execution(* com.touchwisdom.modular.user.user.UserService.deleteUser(..))&&args(id)", returning = "realName")
	public void regiestUser_deleteLog(String id, String realName) {
		logger.info("删除用户写入日志 : " + realName);
		TLog log = new TLog(LogProperty.getText("logtitle.user"),
				LogProperty.getText("logdesc.user.delete", realName),
				userContext.currentUserId(), userContext.currentRealName());
		this.logService.saveEntity(log);
	}

	/**
	 * 添加用户
	 * 
	 * @param user
	 */
	@AfterReturning(value = "execution(* com.touchwisdom.modular.user.user.UserService.saveUser(..))&&args(user)")
	public void regiestLog_createUser(TUser user) {
		logger.info("添加用户写入日志 : " + user.toString());
		TLog log = new TLog(LogProperty.getText("logtitle.user"),
				LogProperty.getText("logdesc.user.create", user.getRealName()),
				userContext.currentUserId(), userContext.currentRealName());
		this.logService.saveEntity(log);
	}

	/**
	 * 修改用户
	 * 
	 * @param user
	 */
	@AfterReturning(value = "execution(* com.touchwisdom.modular.user.user.UserService.updateUser_Aspect(..))&&args(userName)")
	public void regiestLog_updateUser(String userName) {
		logger.info("修改用户写入日志 : " + userName);
		TLog log = new TLog(LogProperty.getText("logtitle.user"),
				LogProperty.getText("logdesc.user.update", userName),
				userContext.currentUserId(), userContext.currentRealName());
		this.logService.saveEntity(log);
	}

	/**
	 * 重置密码
	 * 
	 * @param user
	 */
	@AfterReturning(value = "execution(* com.touchwisdom.modular.user.user.UserService.resetPassword(..))", returning = "userName")
	public void regiestLog_resetUserPassword(String userName) {
		logger.info("用户重置密码写入日志 : " + userName);
		TLog log = new TLog(LogProperty.getText("logtitle.user"),
				LogProperty.getText("logdesc.user.resetpassword", userName),
				userContext.currentUserId(), userContext.currentRealName());
		this.logService.saveEntity(log);
	}

	/**
	 * 删除角色写入日志
	 * 
	 * @param roleId
	 */
	@AfterReturning(value = "execution(* com.touchwisdom.modular.user.role.RoleService.deleteRoleById(..))&&args(roleId)", returning = "roleName")
	public void regiestRole_delete(String roleId, String roleName) {
		logger.info("删除角色写入日志  ： " + roleId);
		TLog log = new TLog(LogProperty.getText("logtitle.role"),
				LogProperty.getText("logdesc.user.delete", roleName),
				userContext.currentUserId(), userContext.currentRealName());
		this.logService.saveEntity(log);
	}

	/**
	 * 添加或者修改用户写入日志
	 * 
	 * @param role
	 * @param roleName
	 */
	@Before(value = "execution(* com.touchwisdom.modular.user.role.RoleService.saveRole(..))&&args(role)")
	public void regiestRole_addOrUpdate(TRole role) {

		TLog log = new TLog();
		log.setLogTitle(LogProperty.getText("logtitle.role"));
		if (role.getId() == null) {
			logger.info("添加角色写入日志  ： " + role.toString());
			log.setLogDesc(LogProperty.getText("logdesc.role.create",
					role.getRoleName()));
		} else {
			logger.info("修改角色写入日志  ： " + role.toString());
			log.setLogDesc(LogProperty.getText("logdesc.role.update",
					role.getRoleName()));
		}
		log.setUserId(userContext.currentUserId());
		log.setUserName(userContext.currentRealName());
		this.logService.saveEntity(log);
	}

	/**
	 * 修改权限写入日志
	 * 
	 * @param role
	 * @param roleName
	 */
	@AfterReturning(value = "execution(* com.touchwisdom.modular.role.service.RolePermissionService.saveRole2MenuId(..))", returning = "roleName")
	public void regiestPermission_update(String roleName) {
		logger.info("修改权限写入日志  ： " + roleName.toString());
		TLog log = new TLog(LogProperty.getText("logtitle.permission"),
				LogProperty.getText("logdesc.permission.update", roleName),
				userContext.currentUserId(), userContext.currentRealName());
		this.logService.saveEntity(log);
	}

}
